package cc.shacocloud.luanniao.core.service.impl

import cc.shacocloud.luanniao.core.model.dto.input.AiModelInput
import cc.shacocloud.luanniao.core.model.po.AiModelPo
import cc.shacocloud.luanniao.core.model.po.NamespacePo
import cc.shacocloud.luanniao.core.repository.AiModelRepository
import cc.shacocloud.luanniao.core.service.AiModelService
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 *
 * @author 思追(shaco)
 */
@Service
@Transactional
class AiModelServiceImpl(
    val aiModelRepository: AiModelRepository,
) : AiModelService {

    /**
     * 根据id查询
     */
    override suspend fun findById(id: Long): AiModelPo? {
        return aiModelRepository.findById(id).awaitSingleOrNull()
    }

    /**
     * 指定id的ai模型是否存在
     */
    override suspend fun existsById(id: Long): Boolean {
        return aiModelRepository.existsById(id).awaitSingle()
    }

    /**
     * 创建ai模型记录
     */
    override suspend fun create(model: AiModelInput, namespace: NamespacePo): AiModelPo {
        val aiModelPo = AiModelPo(
            name = model.name,
            type = model.options.type,
            options = model.options,
            namespaceId = requireNotNull(namespace.id)
        )
        return aiModelRepository.save(aiModelPo).awaitSingle()
    }
}